/*
 * Jn2nView.java
 */
package cz.sevcik.jn2n;

import java.awt.Color;
import org.jdesktop.application.Action;
import org.jdesktop.application.ResourceMap;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;
import org.jdesktop.application.TaskMonitor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.Timer;
import javax.swing.Icon;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.filechooser.FileFilter;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

/**
 * The application's main frame.
 *
 * @author Jaroslav Sevcik
 *
 * @version $Rev: 5 $
 */
public class Jn2nView extends FrameView {

    private Connector connector;
    private boolean connected = false;

    public Jn2nView(SingleFrameApplication app) {
        super(app);

        initComponents();
        // <editor-fold defaultstate="collapsed" desc="Generated Code">
        // status bar initialization - message timeout, idle icon and busy animation, etc
        ResourceMap resourceMap = getResourceMap();
        int messageTimeout = resourceMap.getInteger("StatusBar.messageTimeout");
        messageTimer = new Timer(messageTimeout, new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                statusMessageLabel.setText("");
            }
        });
        messageTimer.setRepeats(false);
        int busyAnimationRate = resourceMap.getInteger("StatusBar.busyAnimationRate");
        for (int i = 0; i < busyIcons.length; i++) {
            busyIcons[i] = resourceMap.getIcon("StatusBar.busyIcons[" + i + "]");
        }
        busyIconTimer = new Timer(busyAnimationRate, new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                busyIconIndex = (busyIconIndex + 1) % busyIcons.length;
                statusAnimationLabel.setIcon(busyIcons[busyIconIndex]);
            }
        });
        idleIcon = resourceMap.getIcon("StatusBar.idleIcon");
        statusAnimationLabel.setIcon(idleIcon);
        progressBar.setVisible(false);
        // </editor-fold>
        initTaskMonitor();
        initTextFieldValue();
        updateButtonState();
    }

    /**
     * Set connection status
     * @param connected
     */
    public void setConnected(boolean connected) {
        this.connected = connected;
        if (connected) {
            showMessage("Connected", MessageType.SUCCESFUL);
        } else {
            showMessage("Disconnected", MessageType.SUCCESFUL);
        }
        updateButtonState();
    }

    /**
     * Show status message
     * @param message
     */
    public void showMessage(String message, MessageType type) {
        switch (type) {
            case SUCCESFUL:
                statusMessageLabel.setForeground(Color.GREEN);
                break;
            case ERROR:
                statusMessageLabel.setForeground(Color.RED);
                break;
            case INFO:
                statusMessageLabel.setForeground(Color.DARK_GRAY);
                break;
        }
        statusMessageLabel.setText(message);
        System.out.println(message);
    }

    @Action
    public void showAboutBox() {
        if (aboutBox == null) {
            JFrame mainFrame = Jn2nApp.getApplication().getMainFrame();
            aboutBox = new Jn2nAboutBox(mainFrame);
            aboutBox.setLocationRelativeTo(mainFrame);
        }
        Jn2nApp.getApplication().show(aboutBox);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        edgeNodeJLabel = new javax.swing.JLabel();
        communityNameJLabel = new javax.swing.JLabel();
        encryptionKeyJLabel = new javax.swing.JLabel();
        superNodeJLabel = new javax.swing.JLabel();
        edgeNodeJTextField = new javax.swing.JTextField();
        communityNameJTextField = new javax.swing.JTextField();
        encryptionKeyJTextField = new javax.swing.JTextField();
        superNodeJTextField = new javax.swing.JTextField();
        connectJButton = new javax.swing.JButton();
        disconnectJButton = new javax.swing.JButton();
        menuBar = new javax.swing.JMenuBar();
        javax.swing.JMenu fileMenu = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JPopupMenu.Separator();
        javax.swing.JMenuItem exitMenuItem = new javax.swing.JMenuItem();
        javax.swing.JMenu helpMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem aboutMenuItem = new javax.swing.JMenuItem();
        statusPanel = new javax.swing.JPanel();
        javax.swing.JSeparator statusPanelSeparator = new javax.swing.JSeparator();
        statusMessageLabel = new javax.swing.JLabel();
        statusAnimationLabel = new javax.swing.JLabel();
        progressBar = new javax.swing.JProgressBar();

        mainPanel.setName("mainPanel"); // NOI18N

        edgeNodeJLabel.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(cz.sevcik.jn2n.Jn2nApp.class).getContext().getResourceMap(Jn2nView.class);
        edgeNodeJLabel.setText(resourceMap.getString("edgeNodeJLabel.text")); // NOI18N
        edgeNodeJLabel.setToolTipText(resourceMap.getString("edgeNodeJLabel.toolTipText")); // NOI18N
        edgeNodeJLabel.setName("edgeNodeJLabel"); // NOI18N

        communityNameJLabel.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        communityNameJLabel.setText(resourceMap.getString("communityNameJLabel.text")); // NOI18N
        communityNameJLabel.setToolTipText(resourceMap.getString("communityNameJLabel.toolTipText")); // NOI18N
        communityNameJLabel.setName("communityNameJLabel"); // NOI18N

        encryptionKeyJLabel.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        encryptionKeyJLabel.setText(resourceMap.getString("encryptionKeyJLabel.text")); // NOI18N
        encryptionKeyJLabel.setToolTipText(resourceMap.getString("encryptionKeyJLabel.toolTipText")); // NOI18N
        encryptionKeyJLabel.setName("encryptionKeyJLabel"); // NOI18N

        superNodeJLabel.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        superNodeJLabel.setText(resourceMap.getString("superNodeJLabel.text")); // NOI18N
        superNodeJLabel.setToolTipText(resourceMap.getString("superNodeJLabel.toolTipText")); // NOI18N
        superNodeJLabel.setName("superNodeJLabel"); // NOI18N

        edgeNodeJTextField.setText(resourceMap.getString("edgeNodeJTextField.text")); // NOI18N
        edgeNodeJTextField.setToolTipText(resourceMap.getString("edgeNodeJTextField.toolTipText")); // NOI18N
        edgeNodeJTextField.setName("edgeNodeJTextField"); // NOI18N

        communityNameJTextField.setText(resourceMap.getString("communityNameJTextField.text")); // NOI18N
        communityNameJTextField.setToolTipText(resourceMap.getString("communityNameJTextField.toolTipText")); // NOI18N
        communityNameJTextField.setName("communityNameJTextField"); // NOI18N

        encryptionKeyJTextField.setText(resourceMap.getString("encryptionKeyJTextField.text")); // NOI18N
        encryptionKeyJTextField.setToolTipText(resourceMap.getString("encryptionKeyJTextField.toolTipText")); // NOI18N
        encryptionKeyJTextField.setName("encryptionKeyJTextField"); // NOI18N

        superNodeJTextField.setText(resourceMap.getString("superNodeJTextField.text")); // NOI18N
        superNodeJTextField.setToolTipText(resourceMap.getString("superNodeJTextField.toolTipText")); // NOI18N
        superNodeJTextField.setName("superNodeJTextField"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(cz.sevcik.jn2n.Jn2nApp.class).getContext().getActionMap(Jn2nView.class, this);
        connectJButton.setAction(actionMap.get("connect")); // NOI18N
        connectJButton.setText(resourceMap.getString("connectJButton.text")); // NOI18N
        connectJButton.setName("connectJButton"); // NOI18N

        disconnectJButton.setAction(actionMap.get("disconnect")); // NOI18N
        disconnectJButton.setText(resourceMap.getString("disconnectJButton.text")); // NOI18N
        disconnectJButton.setName("disconnectJButton"); // NOI18N

        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addGap(16, 16, 16)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addComponent(connectJButton, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(disconnectJButton, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(superNodeJLabel)
                            .addComponent(edgeNodeJLabel)
                            .addComponent(communityNameJLabel)
                            .addComponent(encryptionKeyJLabel))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(edgeNodeJTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 128, Short.MAX_VALUE)
                            .addComponent(communityNameJTextField)
                            .addComponent(encryptionKeyJTextField)
                            .addComponent(superNodeJTextField))))
                .addGap(24, 24, 24))
        );

        mainPanelLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {communityNameJLabel, edgeNodeJLabel, encryptionKeyJLabel, superNodeJLabel});

        mainPanelLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {connectJButton, disconnectJButton});

        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(edgeNodeJTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(edgeNodeJLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(communityNameJTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(communityNameJLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(encryptionKeyJTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(encryptionKeyJLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(superNodeJTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(superNodeJLabel))
                .addGap(18, 18, 18)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(connectJButton)
                    .addComponent(disconnectJButton))
                .addContainerGap())
        );

        mainPanelLayout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {communityNameJLabel, edgeNodeJLabel, encryptionKeyJLabel});

        mainPanelLayout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {communityNameJTextField, edgeNodeJTextField, encryptionKeyJTextField, superNodeJTextField});

        mainPanelLayout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {connectJButton, disconnectJButton});

        superNodeJTextField.getAccessibleContext().setAccessibleDescription(resourceMap.getString("supernodeJTextField.AccessibleContext.accessibleDescription")); // NOI18N

        menuBar.setName("menuBar"); // NOI18N

        fileMenu.setText(resourceMap.getString("fileMenu.text")); // NOI18N
        fileMenu.setName("fileMenu"); // NOI18N

        jMenuItem1.setAction(actionMap.get("load")); // NOI18N
        jMenuItem1.setName("jMenuItem1"); // NOI18N
        fileMenu.add(jMenuItem1);

        jMenuItem2.setAction(actionMap.get("save")); // NOI18N
        jMenuItem2.setName("jMenuItem2"); // NOI18N
        fileMenu.add(jMenuItem2);

        jSeparator1.setName("jSeparator1"); // NOI18N
        fileMenu.add(jSeparator1);

        exitMenuItem.setAction(actionMap.get("quit")); // NOI18N
        exitMenuItem.setName("exitMenuItem"); // NOI18N
        fileMenu.add(exitMenuItem);

        menuBar.add(fileMenu);

        helpMenu.setText(resourceMap.getString("helpMenu.text")); // NOI18N
        helpMenu.setName("helpMenu"); // NOI18N

        aboutMenuItem.setAction(actionMap.get("showAboutBox")); // NOI18N
        aboutMenuItem.setName("aboutMenuItem"); // NOI18N
        helpMenu.add(aboutMenuItem);

        menuBar.add(helpMenu);

        statusPanel.setName("statusPanel"); // NOI18N

        statusPanelSeparator.setName("statusPanelSeparator"); // NOI18N

        statusMessageLabel.setName("statusMessageLabel"); // NOI18N

        statusAnimationLabel.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        statusAnimationLabel.setName("statusAnimationLabel"); // NOI18N

        progressBar.setName("progressBar"); // NOI18N

        javax.swing.GroupLayout statusPanelLayout = new javax.swing.GroupLayout(statusPanel);
        statusPanel.setLayout(statusPanelLayout);
        statusPanelLayout.setHorizontalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(statusPanelSeparator, javax.swing.GroupLayout.DEFAULT_SIZE, 294, Short.MAX_VALUE)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(statusMessageLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 110, Short.MAX_VALUE)
                .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(statusAnimationLabel)
                .addContainerGap())
        );
        statusPanelLayout.setVerticalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addComponent(statusPanelSeparator, javax.swing.GroupLayout.PREFERRED_SIZE, 2, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(statusMessageLabel)
                    .addComponent(statusAnimationLabel)
                    .addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(3, 3, 3))
        );

        setComponent(mainPanel);
        setMenuBar(menuBar);
        setStatusBar(statusPanel);
    }// </editor-fold>//GEN-END:initComponents

    @Action
    public void connect() {
        if (connector != null) {
            connector.disconnect();
        }
        if (validate()) {
            connector = new Connector(createTunnel());
            connector.connect();
        }
    }

    @Action
    public void disconnect() {
        if (connector != null) {
            connector.disconnect();
        }
    }

    @Action
    public void load() {
        File file = chooseFileToOpen();
        if (file != null) {
            System.out.println("Read from " + file.getAbsolutePath() + "\n");

            if (file.exists()) {
                try {
                    JAXBContext context = JAXBContext.newInstance(N2N.class);
                    Unmarshaller unmarshaller = context.createUnmarshaller();
                    N2N tunnel = (N2N) unmarshaller.unmarshal(file);
                    loadTextFieldValue(tunnel);
                    showMessage("Configuration was loaded", MessageType.INFO);

                } catch (JAXBException ex) {
                    if (ex.getMessage().startsWith("unexpected element")) {
                        showMessage("Configuration file is incorrect", MessageType.ERROR);
                    } else {
                        showMessage(ex.getLocalizedMessage(), MessageType.ERROR);
                    }
                }
            } else {
                showMessage("File not found", MessageType.ERROR);
            }
        }
    }

    @Action
    public void save() {
        File file = chooseFileToSave();
        if (file != null) {
            try {
                JAXBContext context = JAXBContext.newInstance(N2N.class);
                Marshaller marshaller = context.createMarshaller();
                marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
                marshaller.marshal(createTunnel(), file);
                showMessage("Configuration was saved", MessageType.INFO);
                System.out.println("Write to " + file.getAbsolutePath());

            } catch (JAXBException ex) {
                showMessage(ex.getLocalizedMessage(), MessageType.ERROR);
            }
        }
    }

    /**
     * Choose file to open
     * @return
     */
    private File chooseFileToOpen() {
        JFileChooser jFileChooser = createFileChooser();
        jFileChooser.setDialogTitle("Open file");
        jFileChooser.setApproveButtonText("Open");
        int result = jFileChooser.showOpenDialog(this.getComponent());

        if (result == JFileChooser.APPROVE_OPTION) {
            return jFileChooser.getSelectedFile();
        }
        return null;
    }

    /**
     * Choose file to save
     * @return
     */
    private File chooseFileToSave() {
        JFileChooser jFileChooser = createFileChooser();
        String extension = ".n2n";

        if (!createTunnel().getCommunityName().isEmpty()) {
            String fileName = createTunnel().getCommunityName() + extension;
            String url = jFileChooser.getCurrentDirectory() + File.separator + fileName;
            File suggestedfile = new File(url);
            jFileChooser.setSelectedFile(suggestedfile);
        }

        jFileChooser.setDialogTitle("Save file");
        jFileChooser.setApproveButtonText("Save");
        int result = jFileChooser.showSaveDialog(this.getComponent());

        if (result == JFileChooser.APPROVE_OPTION) {
            File file = jFileChooser.getSelectedFile();
            String url = file.toString().toLowerCase();

            if (!url.endsWith(extension)) {
                return new File(file.toString() + extension);
            }
            return file;
        }
        return null;
    }

    /**
     * Create file chooser
     * @return
     */
    private JFileChooser createFileChooser() {
        File dir = new File(System.getProperty("user.home") + File.separator + ".jn2n");
        FileFilter filter = new ExtensionFilter("jn2n config (.n2n)", "n2n");

        JFileChooser jFileChooser = new JFileChooser();
        jFileChooser.setCurrentDirectory(dir);
        jFileChooser.addChoosableFileFilter(filter);
        jFileChooser.setFileFilter(filter);
        return jFileChooser;
    }

    /**
     * Create tunnel
     * @return
     */
    private N2N createTunnel() {
        N2N tunnel = new N2N();
        tunnel.setEdgeNode(edgeNodeJTextField.getText());
        tunnel.setCommunityName(communityNameJTextField.getText());
        tunnel.setEncryptionKey(encryptionKeyJTextField.getText());
        tunnel.setSuperNode(superNodeJTextField.getText());
        return tunnel;
    }

    /**
     * Validate input text field
     * @return
     */
    private boolean validate() {
        Validator validator = new Validator();
        if (!validator.validateIP(edgeNodeJTextField.getText())) {
            showMessage("Bad edge node address", MessageType.ERROR);
            return false;
        }

        if (!validator.validateString(communityNameJTextField.getText())) {
            showMessage("Empty community name", MessageType.ERROR);
            return false;
        }

        if (!validator.validateString(encryptionKeyJTextField.getText())) {
            showMessage("Empty encryption key", MessageType.ERROR);
            return false;
        }

        if (!validator.validateIPWidthPort(superNodeJTextField.getText())) {
            showMessage("Bad super node address", MessageType.ERROR);
            return false;
        }
        return true;
    }

    /**
     * Initialization text field values
     */
    private void initTextFieldValue() {
        edgeNodeJTextField.setText("192.168.0.1");
//        communityNameJTextField.setText("myCommunity");
//        encryptionKeyJTextField.setText("myEncryptionKey");
        superNodeJTextField.setText("88.86.108.50:82");
    }

    /**
     * load text field values
     * @param tunnel
     */
    private void loadTextFieldValue(N2N tunnel) {
        edgeNodeJTextField.setText(tunnel.getEdgeNode().toString());
        communityNameJTextField.setText(tunnel.getCommunityName());
        encryptionKeyJTextField.setText(tunnel.getEncryptionKey());
        superNodeJTextField.setText(tunnel.getSuperNode().toString());
    }

    /**
     * Update button state
     */
    private void updateButtonState() {
        disconnectJButton.setEnabled(connected);
    }

    private void initTaskMonitor() {
        // <editor-fold defaultstate="collapsed" desc="Generated Code">
        // connecting action tasks to status bar via TaskMonitor
        TaskMonitor taskMonitor = new TaskMonitor(getApplication().getContext());
        taskMonitor.addPropertyChangeListener(new java.beans.PropertyChangeListener() {

            @Override
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                String propertyName = evt.getPropertyName();
                if ("started".equals(propertyName)) {
                    if (!busyIconTimer.isRunning()) {
                        statusAnimationLabel.setIcon(busyIcons[0]);
                        busyIconIndex = 0;
                        busyIconTimer.start();
                    }
                    progressBar.setVisible(true);
                    progressBar.setIndeterminate(true);
                } else if ("done".equals(propertyName)) {
                    busyIconTimer.stop();
                    statusAnimationLabel.setIcon(idleIcon);
                    progressBar.setVisible(false);
                    progressBar.setValue(0);
                } else if ("message".equals(propertyName)) {
                    String text = (String) (evt.getNewValue());
                    statusMessageLabel.setText((text == null) ? "" : text);
                    messageTimer.restart();
                } else if ("progress".equals(propertyName)) {
                    int value = (Integer) (evt.getNewValue());
                    progressBar.setVisible(true);
                    progressBar.setIndeterminate(false);
                    progressBar.setValue(value);
                }
            }
        });// </editor-fold>
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel communityNameJLabel;
    private javax.swing.JTextField communityNameJTextField;
    private javax.swing.JButton connectJButton;
    private javax.swing.JButton disconnectJButton;
    private javax.swing.JLabel edgeNodeJLabel;
    private javax.swing.JTextField edgeNodeJTextField;
    private javax.swing.JLabel encryptionKeyJLabel;
    private javax.swing.JTextField encryptionKeyJTextField;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JPopupMenu.Separator jSeparator1;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JProgressBar progressBar;
    private javax.swing.JLabel statusAnimationLabel;
    private javax.swing.JLabel statusMessageLabel;
    private javax.swing.JPanel statusPanel;
    private javax.swing.JLabel superNodeJLabel;
    private javax.swing.JTextField superNodeJTextField;
    // End of variables declaration//GEN-END:variables
    private final Timer messageTimer;
    private final Timer busyIconTimer;
    private final Icon idleIcon;
    private final Icon[] busyIcons = new Icon[15];
    private int busyIconIndex = 0;
    private JDialog aboutBox;
}
